; ========================================================================
; XY SQUARING MODE TOGGLE
; ========================================================================
; Purpose: Toggle between auto and manual XY squaring modes
; Parameter R: If present, force enable auto mode (sets initial state to manual, then toggles to auto)
; ========================================================================

; ===== Auto mode not yet available =====
if !exists(param.R)
  M291 S2 R"Feature In Development" P"Automatic XY Squaring mode is currently in development.<br><br>Only <b>Manual</b> mode is available at this time. Automatic mode is coming in a future release."
  M99                                                        ; exit macro

M98 P"0:/sys/user/variables/XySquareManual.g"
M98 P"0:/sys/user/variables/XySquareAuto.g"
M98 P"0:/sys/user/variables/XySquareMode.g"

; ===== If parameter R exists, force initial state to manual (will toggle to auto) =====
if exists(param.R)
  set global.xySquareMode = "manual"

; ===== Toggle the mode =====
if global.xySquareMode == "auto"
  set global.xySquareMode = "manual"
else
  set global.xySquareMode = "auto"

var active = 0.0
if global.xySquareMode == "manual"
  set var.active = global.xySquareManual
else
  set var.active = global.xySquareAuto

echo >"0:/sys/user/variables/XySquareMode.g" "set global.xySquareMode = """^{global.xySquareMode}^""""

echo "XY squaring mode set to "^{global.xySquareMode}^", active offset "^{var.active}^"mm."

; Prompt user to run Auto Calibration when switching to auto mode (skip if parameter R)
if global.xySquareMode == "auto" && !exists(param.R)
  M291 S4 K{"Run Auto Calibration Now", "Skip for Now"} F0 R"Mode Changed to Auto" P"XY Squaring mode has been changed to Auto.<br>To get an accurate value, Auto Calibration is required."
  if input == 0
    M98 P"0:/macros/Auto Calibration"